package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.pojo.BaseResp;
import com.qf.utils.QiNiuUpLoad;
import com.qf.pojo.TbUser;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    QiNiuUpLoad qiNiuUpLoad;

    @RequestMapping("/login")
    @HystrixCommand(commandProperties = {
        @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")},
    fallbackMethod = "getMsgFallback")
    public BaseResp login(@RequestBody User user){
        return userService.login(user);
    }

    public BaseResp getMsgFallback(@RequestBody User user){
        System.out.println("进入了熔断，fallback方法 ");
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(203);
        baseResp.setMessage("网络延迟，请稍后再试");
        return baseResp;
    }
    //退出登录
    @RequestMapping("/outlogin")
    public BaseResp outlogin(HttpServletRequest request){
        return userService.outlogin(request);
    }
    //图片上传
    @RequestMapping("/uploadPic")
    public String upload(@RequestParam("file") MultipartFile multipartFile){
        return qiNiuUpLoad.upLoad(multipartFile);
    }

    //用户数据修改
    @RequestMapping("/updateUser")
    public BaseResp updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
    //注册
    @RequestMapping("/regist")
    public BaseResp regist(@RequestBody TbUser tbUser){
        return userService.regisry(tbUser);
    }
//邮件验证码
    @RequestMapping("/sendEmail")
    public BaseResp sendEmail(@RequestBody Map map){
        return  userService.sendEmail(map.get("email").toString());
    }
    //用户单个查找
    @RequestMapping("/findUserById")
    public BaseResp findUserById(HttpServletRequest request){
        return userService.findUserById(request);
    }
    //用户单个查找
    @RequestMapping("/findUserByToId")
    public BaseResp findUserByToId(@RequestBody Map map){
        return userService.findUserByToId(Integer.valueOf(map.get("id").toString()));
    }
    //用户查找自身粉丝数
    @RequestMapping("/findFansAll")
    public BaseResp findFansAll(HttpServletRequest request){
        return userService.findFansAll(request);
    }
   //用户分享内容查找
    @RequestMapping("/findUserShareByUid")
    public BaseResp findUserShareByUid( HttpServletRequest request){
        return userService.findUserShareByUid(request);
    }
    //用户分享内容的删除
    @RequestMapping("/delePicShare")
    public BaseResp delePicShare( @RequestBody Map map){
        return userService.delePicShare(map);
    }
}
